﻿using System;
using System.Net;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using RestSharp;
using SIS.Android.Services.Common;

namespace SIS.Android.Activity
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class LoginActivity : AppCompatActivity
    {
        private EditText _etName;
        private EditText _etPassword;
        private Button _btnlogin;
        private TextView _btnregister;
        private CheckBox _cbPassword;
        private CheckBox _cbLogin;
        private TextView _btnPhoneLogin;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_login);
            InitViews();
            _btnlogin.Click += LoginBtn_Click;
            _btnregister.Click += _btnregister_Click;
            _btnPhoneLogin.Click += BtnPhoneLogin_Click;
        }

        private void BtnPhoneLogin_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(PhoneLoginActivity));
            StartActivity(intent);
        }

        private void _btnregister_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(RegisterActivity));
            StartActivity(intent);
        }

        private void LoginBtn_Click(object sender, EventArgs e)
        {

            var restSharp = new RestSharpClient("http://114.116.64.59/sis/api/");
            var request = new RestRequest("Login", Method.POST);
            var postdata = new
            {
                Name = _etName.Text,
                PassWord = _etPassword.Text,
            };
            var json = request.JsonSerializer.Serialize(postdata);
            request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
          
            var respose = restSharp.Execute(request);

            if (respose.StatusCode == HttpStatusCode.OK)
            {
                var v = respose.Content;
                if (v == "0")
                {
                    //RunOnUiThread(() => Toast.MakeText(this, "登录成功", ToastLength.Short).Show());
                    Intent intent = new Intent(this, typeof(MainActivity));
                    StartActivity(intent);
                }
                else
                {
                    RunOnUiThread(() => Toast.MakeText(this, "登录失败：用户名或密码错误", ToastLength.Short).Show());
                }
            }
            else
            {
                RunOnUiThread(() => Toast.MakeText(this, "获取失败：" + respose.StatusCode.ToString(), ToastLength.Short).Show());
            }     

        }

        private void InitViews()
        {
            _btnlogin = (Button)FindViewById(Resource.Id.btn_login);
            _etName = (EditText)FindViewById(Resource.Id.et_account);
            _etPassword = (EditText)FindViewById(Resource.Id.et_password);
            _cbPassword = (CheckBox)FindViewById(Resource.Id.checkBox_password);
            _cbLogin = (CheckBox)FindViewById(Resource.Id.rb_female);
            _btnregister = (TextView)FindViewById(Resource.Id.btn_register);
            _btnPhoneLogin = (TextView)FindViewById(Resource.Id.btn_phoneLogin);
           // _textView = (TextView)FindViewById(Resource.Id.txtvErrorMessage);
            //iv_see_password = (ImageView)FindViewById(Resource.Id.iv_see_password);
        }

        
    }
}